package com.hancai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hancai.constant.Constants;
import com.hancai.mapper.TClientMapper;
import com.hancai.model.TClient;
import com.hancai.query.ClientQuery;
import com.hancai.service.ClientService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 */
@Service
public class ClientServiceImpl implements ClientService {

    @Resource
    private TClientMapper tClientMapper;

    @Override
    public PageInfo<TClient> getClientByPage(Integer current,String keyword) {
        //1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //2.查询
        List<TClient> list = tClientMapper.selectClientPage(keyword);
        //3.封装分页数据到PageInfo
        PageInfo<TClient> info = new PageInfo<>(list);
        return info;
    }


    @Override
    public int saveClient(ClientQuery clientQuery) {
        TClient tClient = new TClient();
        //给客户设置默认密码,当客户密码不为空时
        if(!StringUtils.hasText(clientQuery.getPassword())){
            clientQuery.setPassword(Constants.DEFAULT_CLIENT_PASSWORD);
        }
        if(!StringUtils.hasText(clientQuery.getAvatar())){
            clientQuery.setAvatar(Constants.DEFAULT_CLIENT_AVATAR);
        }
        clientQuery.setStatus(TClient.STATUS_NORMAL);
        clientQuery.setDeleted(TClient.NO_DELETED);
        //把clientQuery对象里面的属性数据复制到client对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(clientQuery, tClient);
        //创建时间
        tClient.setCtime(new Date());
        tClient.setUtime(new Date());
        return tClientMapper.insertSelective(tClient);
    }

    @Override
    public int updateClient(ClientQuery clientQuery) {
        TClient tClient = new TClient();
        //把clientQuery对象的属性数据复制到client对象(复制要求：两个类的字段名和类型相同)
        BeanUtils.copyProperties(clientQuery,tClient);
        tClient.setUtime(new Date());
        return tClientMapper.updateByPrimaryKeySelective(tClient);
    }

    @Override
    public int delClientById(Integer id) {
        return tClientMapper.deleteByPrimaryKey(id);
    }


    @Override
    public void changeStatus(Integer id, Integer status) {
        tClientMapper.changeStatus(id,status);
    }

    @Override
    public TClient getClientById(Integer id) {
        return tClientMapper.selectDetailById(id);
    }

    @Override
    public List<TClient> getAllOnSaleProduct() {
        return tClientMapper.selectAllClient();
    }




}
